﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using YAPL.Language.Ast.Utils;

namespace YAPL.Language.Ast {
	public class AssignmentExpression : Expression {
		private Expression _left;
		private Expression _right;

		public AssignmentExpression(ExpressionType nodeType, Type type, Expression left, Expression right) : base(nodeType, type) {
			_left = left;
			_right = right;
		}

		public AssignmentExpression(ExpressionType nodeType, Type type, bool canRead, bool canWrite, Expression left, Expression right)
			: base(nodeType, type, canRead, canWrite) {
			_left = left;
			_right = right;
		}

		[Descendant(AstVisit.BeforeCurrent, Priority = 0)]
		public Expression Left {
			get { return _left; }
		}

		[Descendant(AstVisit.BeforeCurrent, Priority = 1)]
		public Expression Right {
			get { return _right; }
		}

		public override bool NodeEquals(Expression other) {
			return other is AssignmentExpression;
		}

		public override bool RewriteChild(Expression child, Expression newExpression) {
			var rewritten = false;
			if (ReferenceEquals(_left, child)) {
				_left = newExpression;
				rewritten = true;
			}
			if (ReferenceEquals(_right, child)) {
				_right = newExpression;
				rewritten = true;
			}
			return rewritten;
		}
	}
}
